package com.hundsun.yr.universal.p2p.ui.view;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.hundsun.yr.universal.library.widget.HSButton;
import com.hundsun.mobile.zwgt.R;

/**
 * 项目中的自定义dialog
 * @author tonglj
 *
 */
@SuppressLint("InflateParams") public class HSCustomDialog implements OnClickListener {
	public final static int  LEFTBUTTON = 0;
	public final static int  RIGHTBUTTON = 1;
	public static final int  CLOSEBUTTON = 2;
	private LayoutInflater inflater;
	private Context context;

//	private TextView tvMsg;
//	private CharSequence message;
//	private TextView tvTitle;
//	private CharSequence title;
//	private Button btnLeft,btnRight;
//	private CharSequence rightBtnText;
	
	private View contentView;
	private OnDialogButtonClick listener;
	private final Dialog dialog ;
	private View btnClose;
//	private View titleBar;
	/**
	 * 布局
	 */
	private Style style;
	
	public enum Style {
		loan_formula,
		/**
		 * 转让-初始-预计转让所得公式
		 */
		transfer_get,
		/**
		 * 转让-初始-转让单价
		 */
		transfer_unitprice,
		/**
		 * 提现手续费
		 */
		widthdraw_counterFee
		
	}

	public HSCustomDialog(Context context,Style style) {
		this.context = context;
		this.style = style;
		inflater = LayoutInflater.from(context);
		if(style  == Style.loan_formula){
			contentView = inflater.inflate(R.layout.view_loan_formula_dialog, null);
		}else if(style  == Style.transfer_get){
			contentView = inflater.inflate(R.layout.view_transferget_dialog, null);
		}else if(style  == Style.transfer_unitprice){
			contentView = inflater.inflate(R.layout.view_transferprice_dialog, null);
		}else if(style == Style.widthdraw_counterFee){
			contentView = inflater.inflate(R.layout.view_widthdraw_counterfee_dialog, null);
		}
		// 新建对话框对象
		 dialog = new AlertDialog.Builder(context).create();
		 dialog.setCancelable(false);
		 dialog.show();
		 findView();
		 dialog.getWindow().setContentView(contentView);
		 //设置监听
		 btnClose.setOnClickListener(this);
		 
	}
	
	private void findView(){
//		tvMsg = (TextView) contentView.findViewById(R.id.dialog_msg);
//		tvTitle = (TextView) contentView.findViewById(R.id.dialog_title);
//		btnLeft = (Button) contentView.findViewById(R.id.dialog_btn_left);
//		btnRight = (Button) contentView.findViewById(R.id.dialog_btn_right);
//		titleBar =  contentView.findViewById(R.id.dialog_titlebar);
		btnClose =  contentView.findViewById(R.id.dialog_loan_close);
		
	}
	/**
	 * 
	 * @param style
	 * @param values
	 */
	public void setValue(String...values){
		if(style == Style.loan_formula){
			TextView tv_invest = (TextView) contentView.findViewById(R.id.invest);
			TextView tv_serviceAmount = (TextView) contentView.findViewById(R.id.service_amount);
			TextView tv_serviceRate = (TextView) contentView.findViewById(R.id.service_rate);
			TextView tv_cost = (TextView) contentView.findViewById(R.id.cost);
			TextView tv_formula = (TextView) contentView.findViewById(R.id.rate_formula);
			tv_invest.setText(context.getString(R.string._rmb,values[0]));
			tv_serviceAmount.setText(context.getString(R.string._rmb,values[1]));
			tv_serviceRate.setText(context.getString(R.string.rate_for,values[2])+"%");
			tv_cost.setText(context.getString(R.string._rmb,values[3]));
			tv_formula.setText(context.getString(R.string.rate_formula,values[4],values[5]));
			
		}else if(style == Style.transfer_get){
			//预计转让所得
			TextView tv_transfGet = (TextView) contentView.findViewById(R.id.dialog_transf_get);
			TextView tv_counterFee = (TextView) contentView.findViewById(R.id.dialog_counter_fee);
			HSButton btn_Submit = (HSButton) contentView.findViewById(R.id.btn_dialog_transfer);
			tv_counterFee.setText(context.getString(R.string.counter_fee_,values[0]));
			tv_transfGet.setText(context.getString(R.string._rmb,values[1]));
			btn_Submit.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if(listener != null){
						listener.click(10);
					}
				}
			});
			
		}else if(style == Style.transfer_unitprice){
			TextView tv_fairPrice = (TextView) contentView.findViewById(R.id.dialog_fairprice);
			TextView tv_market = (TextView) contentView.findViewById(R.id.dialog_market);
			String  daymarketContent = context.getString(R.string.dialog_daymarket_content,values[0]);
			SpannableStringBuilder builder = new SpannableStringBuilder(daymarketContent);
			builder.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.primary_cl_2)), daymarketContent.indexOf(values[0]),builder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
			tv_fairPrice.setText(builder);
			tv_market.setText(context.getString(R.string.dialog_daymarket,values[1]));
		}else if(style == Style.widthdraw_counterFee){
			TextView tv_formula = (TextView) contentView.findViewById(R.id.counterfee_formula);
			TextView tv_counterFee = (TextView) contentView.findViewById(R.id.counter_fee);
			tv_formula.setText(context.getString(R.string.widthdraw_counterfee_formula, values[0]));
			tv_counterFee.setText(context.getString(R.string._counterfee_, values[1],values[2]));
		}
	}

	
//	public void show(){
//		tvMsg.setText(message);
//		tvTitle.setText(title);
//		btnRight.setText(rightBtnText);
//	}
	
	

	/**
	 * 设置对话框界面
	 *           
	 * @return
	 */
	public void setContentView(View v) {
		this.contentView = v;
	}
	

	public void setBtnClickListener(OnDialogButtonClick listener){
		this.listener = listener;
	}

	@Override
	public void onClick(View v) {
		dialog.dismiss();
		if(listener != null){
			if (v.getId() == R.id.dialog_btn_left) {
				listener.click(LEFTBUTTON);
			}else if(v.getId() == R.id.dialog_btn_right){
				listener.click(RIGHTBUTTON);
			}else if(v.getId() == R.id.dialog_close_ic){
				listener.click(CLOSEBUTTON);
			}
		}
		
	}
	
	
	public interface OnDialogButtonClick{
		public void click(int witch);
	}

}